package com.efast.cafe.commponent.es.tcp.utils;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * byte[]与16进制字符串相互转换
 * @author fxm
 *
 */
public class BytesHexStrTranslate {
	private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', 
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /**
     * 方法一：
     * byte[] to hex string
     * 
     * @param bytes
     * @return
     */
    public static String bytesToHexFun1(byte[] bytes) {
        // 一个byte为8位，可用两个十六进制位标识
        char[] buf = new char[bytes.length * 2];
        int a = 0;
        int index = 0;
        for(byte b : bytes) { // 使用除与取余进行转换
            if(b < 0) {
                a = 256 + b;
            } else {
                a = b;
            }

            buf[index++] = HEX_CHAR[a / 16];
            buf[index++] = HEX_CHAR[a % 16];
        }

        return new String(buf);
    }

    /**
     * 方法二：
     * byte[] to hex string
     * 
     * @param bytes
     * @return
     */
    public static String bytesToHexFun2(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        for(byte b : bytes) { // 利用位运算进行转换，可以看作方法一的变种
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }

        return new String(buf);
    }

    /**
     * 方法三：
     * byte[] to hex string
     * 
     * @param bytes
     * @return
     */
    public static String bytesToHexFun3(byte[] bytes) {
        StringBuilder buf = new StringBuilder(bytes.length * 2);
        for(byte b : bytes) { // 使用String的format方法进行转换
            buf.append(String.format("%02x", new Integer(b & 0xff)));
        }

        return buf.toString();
    }

    /**
     * 将16进制字符串转换为byte[]
     * 
     * @param str
     * @return
     */
    public static byte[] toBytes(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }
    
    
    /**  
     * byte数组中取int数值，本方法适用于(低位在前，高位在后)的顺序，和和intToBytes（）配套使用 
     *   
     * @param src  
     *            byte数组  
     * @param offset  
     *            从数组的第offset位开始  
     * @return int数值  
     */    
 public static int bytesToInt(byte[] ary, int offset) {  
	 int value;    
	    value = (int) ((ary[offset]&0xFF)   
	            | ((ary[offset+1]<<8) & 0xFF00)  
	            | ((ary[offset+2]<<16)& 0xFF0000)   
	            | ((ary[offset+3]<<24) & 0xFF000000));  
	    return value; 
 }  
 
 
 public static long Byte4_To_long(byte [] content,int offset)
	{
		long v=0;
		
		v=		(long)((long)(content[offset+0] & 0xFF) | 
				((long)(content[offset+1] & 0xFF)<<8)|
				((long)(content[offset+2] & 0xFF)<<16)|
				((long)(content[offset+3] & 0xFF)<<24));
		
		return v;
	}
 
 public static long Byte2_To_long(byte [] content,int offset)
	{
		long v=0;
		
		v=		(long)((long)(content[offset+0] & 0xFF) | 
				((long)(content[offset+1] & 0xFF)<<8));
		
		return v;
	}
 
 public static long Byte1_To_long(byte content)
	{
		long v=0;
		
		v=		(long)((long)(content & 0xFF));
		
		return v;
	}
 
   
  /**  
     * byte数组中取int数值，本方法适用于(低位在后，高位在前)的顺序。和intToBytes2（）配套使用 
     */  
 public static int bytesToInt2(byte[] bytes, int offset) {  
	 int low = bytes[offset];  
     int high = bytes[offset + 1];  
     return (high << 8 & 0xFF00) | (low & 0xFF); 
 }  
 
 	public static int lheserve(int javaReadInt){  
 	// 将每个字节取出来  
 	   byte byte4 = (byte) (javaReadInt & 0xff);  
 	   byte byte3 = (byte) ((javaReadInt & 0xff00) >> 8);  
 	   byte byte2 = (byte) ((javaReadInt & 0xff0000) >> 16);  
 	   byte byte1 = (byte) ((javaReadInt & 0xff000000) >> 24);  
 	     
 	   // 拼装成 "高字节在后，低字节在前"的格式
 	   int realint = (byte1& 0xff)<<0  |(byte2& 0xff)<<8 | (byte3& 0xff)<< 16| (byte4& 0xff)<<24 ; 
 	   	
 	     return realint;  
 	 }
    
 	public static byte[] calendar2Bytes(long millis) {
		int time = (int)(millis/1000);
		byte[] bytes = new byte[4];
		for(int i = bytes.length - 1; i >= 0; i--) {
			bytes[i] = (byte)(time & 0xFF);
			time >>= 8;
		}
		return bytes;
	}
	
	public static Calendar bytes2Calendar(byte[] bytes) {
		int time = (bytes[0] << 24) & 0xFF000000 |
					(bytes[1] << 16) & 0xFF0000 |
					(bytes[2] << 8) & 0xFF00 |
					(bytes[3]) & 0xFF;
		Calendar calendar = new GregorianCalendar();
		calendar.setTimeInMillis(time * 1000L);
		return calendar;
	}
 
    
    public static void main(String[] args) throws Exception {
      //  byte[] bytes = "测试".getBytes("utf-8");
      //  System.out.println("字节数组为：" + Arrays.toString(bytes));
      //  System.out.println("方法一：" + bytesToHexFun1(bytes));
      //  System.out.println("方法二：" + bytesToHexFun2(bytes));
       // System.out.println("方法三：" + bytesToHexFun3(bytes));

       // System.out.println("==================================");

     //   String str = "05383633303836303334353638323636000B000B0310000000001D311100";
        //System.out.println("转换后的字节数组：" + Arrays.toString(toBytes(str)));
    	
       
        byte [] content = new byte[]{0x7e,0x05,0x01,0x00,0x14,0x00
        		,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
        		,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7e};
        long res = BytesHexStrTranslate.bytesToInt2(content,0);
        System.out.println(res);
       // System.out.println(new String(toBytes(str), "utf-8"));
    }

}
